package com.juzi.wopu.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.juzi.wopu.domain.Employee;
import com.juzi.wopu.service.IEmployeeService;

public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService{

	@Override
	public boolean findByUsername(String username) {
		String hql="select count(o) from Employee o where o.username=?";
		Long countList = (Long)BaseDao.findByhql(hql, username).get(0);
		if(countList.intValue()>0){
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Employee findByLoginUser(String username, String password) {
		String sql="select o from Employee o where o.username=? and o.password=?";
		List<Employee> list = BaseDao.findByhql(sql, username,password);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("resource")
	@Override
	public InputStream imp(String[] heads, List<String[]> data) throws Exception {
		SXSSFWorkbook wb=new SXSSFWorkbook();
		Sheet sheet = wb.createSheet();
		Row headRow = sheet.createRow(0);
		for(int i=0;i<heads.length;i++){
			Cell cell = headRow.createCell(i);
			cell.setCellValue(heads[i]);
		}

		for(int i = 0;i<data.size();i++){
			String[] rowData=data.get(i);
			Row row = sheet.createRow(i+1);
			for(int j=0;j<heads.length;j++){
				Cell cell = row.createCell(j);
				cell.setCellValue(rowData[j]);
			}
		}

		ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		wb.write(outputStream);
		outputStream.close();
		wb.dispose();
		InputStream inputStream=new ByteArrayInputStream(outputStream.toByteArray());
		return inputStream;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> getBuyers() {
		return BaseDao.findByhql("select o from Employee o where o.department.name=?", "采购部");
	}

	@Override
	public void changePassword(Long id,String password) {
		String sql="update tSysEmployee set password='"+password+"' where id="+id;
		BaseDao.executeSql(sql); 
	}	
}